from sqlalchemy import MetaData
from setting import engine, Base
import models

def tables():
	m = MetaData()
	m.reflect(bind=engine)
	return m.tables

def get_table(table):
	m = MetaData()
	m.reflect(bind=engine)
	name = None

	if isinstance(table, str):
		name = table
	elif issubclass(table, Base):
		name = table.__tablename__

	return m.tables[name]

def get_custom(name, session):
	c = session.query(models.Custom).filter(models.Custom.name==name).all()
	if len(c):
		return c[0]
	c = models.Custom(name)
	session.add(c)
	return c

def get_grade(name, session):
	c = session.query(models.Grade).filter(models.Grade.name==name).all()
	if len(c):
		return c[0]
	c = models.Grade(name)
	session.add(c)
	return c


